<?php
class Cms_IndexController extends Zend_Controller_Action {
    private $_languages = array();
    public function init() {
        $this->_languages = Utils_Global::getSupportLanguages();    
    }
    
	public function indexAction() {
	    $metaInfo = array('title_label' => $this->view->translate('fmit_home_title'),
	                        'description_label' => $this->view->translate('fmit_home_description'),
	                        'keyword_label' => $this->view->translate('fmit_home_keyword'),
	    );
		$this->view->metadata = $this->view->metadata('home', $metaInfo,
									$this->view->serverUrl() . $canonical);
		Utils_Global::$params['homepageclzz'] = 'current';
	}
	
	public function changeLangAction() {
	    $lang = Utils_Global::$params['lang'];
	    if(in_array($lang, $this->_languages)) {
	        $_SESSION['locale'] = $lang;
	    }
	    
	    $previousUrl = $_SERVER['HTTP_REFERER'];
	    $this->_redirect($previousUrl);
	}
	
	public function courseAction() {
	    $limit = Utils_Global::$params['limit'];
	    $lang = Utils_Global::$params['lang'];
	    if(!$limit || $limit < 0) {
	        $limit = 7; 
	    }
	    if(!in_array($lang, $this->_languages)) {
	        $lang = Utils_Global::getCurrentLanguage();
	    }
	    $model = Cms_Model_Course::factory();
	    $options = array('offset' => 0, 'limit' => $limit, 'lang' => $lang);
	    $courses = $model->getCourses($options);
	    $this->view->courses = $courses;
	    $this->view->lang = $lang;
	}
	
	public function goingStartedCourseAction() {
	    $limit = Utils_Global::$params['limit'];
	    $lang = Utils_Global::$params['lang'];
	    if(!$limit || $limit < 0) {
	        $limit = 6;
	    }
	    
	    if(!in_array($lang, $this->_languages)) {
	        $lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    $model = Cms_Model_Course::factory();
	    $options = array('offset' => 0, 'limit' => $limit, 'lang' => $lang, 'order' => 'last_update', 'by' => 'DESC');
	    $courses = $model->getCourses($options);
	    $this->view->courses = $courses;
	    $this->view->lang = $lang;
	}
	
	public function articleAction() {
	    $limit = Utils_Global::$params['limit'];
	    $lang = Utils_Global::$params['lang'];
	    if(!$limit || $limit < 0) {
	        $limit = 6;
	    }
	    if(!in_array($lang, $this->_languages)) {
	        $lang = Utils_Global::getCurrentLanguage();
	    }
	    $model = Cms_Model_Article::factory();
	    $options = array('offset' => 0, 'limit' => $limit, 'lang' => $lang, 'exclude_cate' => array(2,4));
	    $articles = $model->getArticles($options);
	    $this->view->articles = $articles;
	    $this->view->lang = $lang;
	    $this->view->staticUrl =  Utils_Global::get('staticUrl') . '/article/';
	}
	
	public function subscribeAction() {
	    $fullname = trim(Utils_Global::$params['fullname']);
	    $email = trim(Utils_Global::$params['email']);
	    $ip = $_SERVER['REMOTE_ADDR'];
	    if(!$fullname) {
	        $result = array('code' => 0, 'message' => 'Vui lòng điền tên!');
	        $this->_helper->json($result);
	    }
	    if(!$email || !Utils_CommonFunction::isValidEmail($email)) {
	        $result = array('code' => 0, 'message' => 'Vui lòng điền email hợp lệ!');
	        $this->_helper->json($result);
	    }
	    $model = Cms_Model_Subscriber::factory();
	    $data = array('full_name' => $fullname, 'email' => $email, 
	                    'ip' => $ip, 'active' => 1, 'last_update' => time(),
	                    'dateline' => time(),
	    );
	    $result = $model->insert($data);
	    if($result == 0) {
	        $result = array('code' => 0, 'message' => 'Có lỗi xảy ra');
	    } else if($result == -1) {//Da dang ky
	        $result = array('code' => -1, 'message' => 'Email này đã được đăng ký!');
	        $this->_helper->json($result);
	    } else {
	        $result = array('code' => 1, 'message' => 'Đăng ký thành công!');
	        $this->_helper->json($result);
	    }
	}
	
	public function unsubscribeAction() {
		$email = trim(Utils_Global::$params['email']);
		$model = Cms_Model_Subscriber::factory();
		$data = array('active' => 0, 'last_update' => time(),);
		$result = $model->update($email, $data);
		
		if($result == 0) {
		    $this->_redirect($this->view->serverUrl() . '/unsubscribe-fail.html');
		} else {
		    $this->_redirect($this->view->serverUrl() . '/unsubscribe-success.html');
		}
	}
	
	public function subscribeBoxAction() {
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	        $lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    $this->view->lang = $lang;
	}
	
	public function workshopAction() {
	    $limit = Utils_Global::$params['limit'];
	    $course = $this->_request->getParam('course');
	    $lang = Utils_Global::$params['lang'];
	    if(!$limit || $limit < 0) {
	    	$limit = 3;
	    }
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    $model = Cms_Model_Article::factory();
	    $options = array('offset' => 0, 'limit' => $limit, 'lang' => $lang, 'category' => 2);
	    $locations = Utils_Global::getLocations();
	    $options['location'] =  $locations['hcm'];
	    $hcmWorkshops = $model->getWorkshops($options);
	    $options['location'] = $locations['hn'];
	    $hnWorkshops = $model->getWorkshops($options);
	    $this->view->hcmWorkshops = $hcmWorkshops;
	    $this->view->hnWorkshops = $hnWorkshops;
	    
	    $options = array('offset' => 0, 'limit' => 3, 'lang' => $lang, 'category' => 4);
	    $scholarships = $model->getArticles($options);
	    $this->view->scholarships = $scholarships;
	    $this->view->lang = $lang;
	    $this->view->course = $course;
	    Utils_Global::$params['course'] = $course;
	}
	
	public function contactAction() {
	    $lang = Utils_Global::$params['lang'];
	    if(!in_array($lang, $this->_languages)) {
	    	$lang = Utils_Global::getCurrentLanguage();
	    }
	    
	    $model = Cms_Model_Page::factory();
	    $option = array('id' => 11, 'lang' => $lang);
	    $page = $model->select($option);
	    if($page) {
	        $page = $page[0];
	    }
	    $breadcrumbData = array(
	    		array('url' => $this->view->serverUrl(),
	    				'displayName' => $this->view->translate('home', $lang),
	    				'title' => $this->view->translate('home', $lang),
	    		),
	    		array(
	    				'url' => 'javascript:void(0)',
	    				'displayName' => $this->view->translate('contact_title', $lang),
	    				'title' => $this->view->translate('contact_title', $lang),
	    		),
	    );
	    
	    $this->view->breadcrumb = $breadcrumbData;
	    $metaInfo = array('contact_title' => $this->view->translate('contact_title', $lang),
	    );
	    $this->view->metadata = $this->view->metadata('fmit_contact', $metaInfo,
	    		$this->view->serverUrl() . $this->view->url(array()));
	    $this->view->lang = $lang;
	    $this->view->address = $page['content'];
	}
	
	public function sendContactAction() {
	    $fullname = trim(Utils_Global::$params['fullname']);
	    $email = trim(Utils_Global::$params['email']);
	    $phone = trim(Utils_Global::$params['phone']);
	    $subject = trim(Utils_Global::$params['subject']);
	    $content = trim(Utils_Global::$params['content']);
	    
	    if(!$fullname) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập họ tên';
	    	$this->_helper->json($result);
	    }
	    if(!$email || !Utils_CommonFunction::isValidEmail($email)) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập email hợp lệ';
	    	$this->_helper->json($result);
	    }
	    if(!$phone || !Utils_CommonFunction::isValidPhone($phone)) {
	    	$result['code'] = 0;
	    	$result['message'] = 'Vui lòng nhập số điện thoại hợp lệ';
	    	$this->_helper->json($result);
	    }
	    if(!$subject) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng nhập tiêu đề liên hệ';
	        $this->_helper->json($result);
	    }
	    if(!$content) {
	        $result['code'] = 0;
	        $result['message'] = 'Vui lòng nhập nội dung liên hệ';
	        $this->_helper->json($result);
	    }
	    
	    //Render email
	    $this->view->fullname = $fullname;
	    $this->view->email = $email;
	    $this->view->phone = $phone;
	    $this->view->subject = $subject;
	    $this->view->content = $content;
	    $emailContent = $this->view->render('index/contact-email-content.phtml');
	    $fmitEmail = Utils_Global::getConfig('cms', 'application', 'fmitemail');
	    $fmitEmail = $fmitEmail->toArray();
	    $sent = Utils_CommonFunction::sendMail($fmitEmail, 'Liên hệ', $emailContent);
	    if($sent) {
	    	$result['code'] = 1;
	    	$result['message'] = "Chúc mừng bạn đã gửi thông tin liên hệ thành công!";
	    	$this->_helper->json($result);
	    } else {
	    	$result['code'] = 0;
	    	$result['message'] = "Có lỗi xảy ra, vui lòng thử lại!";
	    	$this->_helper->json($result);
	    }
	}
	
	public function testAction() {
		// 		prBinh(serialize(array('categories' => '3,4,5,6','category' => 7)));
		// 		$cateogryModel = Cms_Model_Category::factory();
		// 		prBinh($cateogryModel->getCategories(array(2,1)));
		// 		$articleModel = Cms_Model_Article::factory();
		// 		prBinh($articleModel->getArticleByCategory(2));
		// 		prBinh(serialize(array('position' => json_encode(array(1,2)))));
		// 		prBinh(serialize(array('position' =>12)));
		prBinh(Utils_Global::hashName('323232424242424242432dfwer32r23ed2332'));
	}
}
